import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ConfirmationService, SelectItem } from 'primeng/primeng';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { ConfigService } from './cfg.service';

@Component({
  selector: 'baseinfo',
  templateUrl: './baseinfo.component.html',
  styleUrls: ['./baseinfo.component.scss'],
  providers: []
})
export class BaseInfoComponent implements OnInit {
  taskList: any = {};
  taskListForm: FormGroup;
  pageSize = [
    { label: '30条', value: 30 },
    { label: '50条', value: 50 },
    { label: '100条', value: 100 }
  ];
  icons: SelectItem[] = new Array(
    { label: 'fa-circle-o', value: 'fa fa-circle-o' },
    { label: 'fa-cogs', value: 'fa fa-cogs' },
    { label: 'fa-university', value: 'fa fa-university' },
    { label: 'fa-database', value: 'fa fa-database' },
    { label: 'fa-address-book-o', value: 'fa fa-address-book-o' }
  );

  @Input()
  bizId: number = 0;

  @Input()
  systemUrl: string = '';
  
  @Input()
  set taskListId(taskListId: number) {
    if (taskListId > 0 && this.systemUrl) {
      this.configService.showTaskListDetail(taskListId, this.systemUrl).subscribe(taskList => {
        this.taskList = taskList;
        this.buildForm();
      });
    } else { //添加
      this.taskList = {};
      this.buildForm();
    }
  }

  get taskListId(): number { return this.taskList.taskListId; }

  constructor(
    private fb: FormBuilder,
    private configService: ConfigService,
    private mdlSnackbarService: MdlSnackbarService,
    private confirmationService: ConfirmationService) {
  }

  ngOnInit() {
    this.buildForm();
  }

  saveUpdate(): void {
    let taskList = this.taskListForm.value;
    taskList['bizCode'] = 1;
    taskList.countVisible = taskList.countVisible ? 1 : 0;
    this.configService.saveUpdateTaskList(taskList, this.systemUrl).subscribe(data => {
      this.configService.announceMission(data + "");
      this.mdlSnackbarService.showToast("保存栏目成功！");
    });
  }

  delete(): void {
    this.confirmationService.confirm({
      header: '确认',
      message: '您确定要删除当前栏目记录吗？',
      accept: () => {
        this.configService.deleteTaskList(this.taskListId, this.systemUrl).subscribe(data => {
          this.configService.announceMission(this.taskListId + "");
          this.mdlSnackbarService.showToast("删除栏目成功！");
        });
      }
    });
  }

  buildForm(): void {
    this.taskListForm = this.fb.group({
      'groupId': [this.taskList.groupId],
      'taskListId': [this.taskList.taskListId],
      'taskListName': [this.taskList.taskListName, [
        Validators.required,
        Validators.maxLength(25)
      ]
      ],
      'countPerPage': [this.taskList.countPerPage],
      'countVisible': [this.taskList.countVisible],
      'displayIcon': [this.taskList.displayIcon],
      'displayOrder': [this.taskList.displayOrder],
      'taskListDesc': [this.taskList.taskListDesc],
      'taskListSql': [this.taskList.taskListSql, [
        Validators.required,
        Validators.minLength(100)
      ]]
    });

    this.taskListForm.valueChanges
      .subscribe(data => this.onValueChanged(data));
    this.onValueChanged();
  }

  onValueChanged(data?: any) {
    if (!this.taskListForm) { return; }
    const form = this.taskListForm;

    for (const field in this.formErrors) {
      this.formErrors[field] = '';
      const control = form.get(field);

      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }
  }

  formErrors = {
    'taskListName': '',
    'displayOrder': '',
    'taskListDesc': '',
    'taskListSql': ''
  };


  validationMessages = {
    'taskListName': {
      'required': '名称不能为空',
      'maxlength': '名称长度不能超过25个字符'
    },
    'displayOrder': {
      'pattern': '排序位置必须为数字'
    },
    'taskListDesc': {
      'required': '功能描述不能为空'
    },
    'taskListSql': {
      'required': '开箱SQL不能为空',
      'minLength': 'SQL语句太短'
    }
  };
}
